package com.macoli.android.permissionx

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import android.Manifest
import android.widget.TextView
import java.lang.StringBuilder

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.request_permission_btn).setOnClickListener {
            lifecycleScope.launch {
                val permissionList = arrayListOf<String>(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                )
                val permissionXResultMap = requestPermissions(this@MainActivity , permissionList)

                findViewById<TextView>(R.id.all_granted_tv).setText("${permissionXResultMap.allGranted}")

                val resultSb = StringBuilder()

                permissionXResultMap.forEach { t, u ->
                    resultSb.append("permission $t: granted = ${u.granted} , hasRefuse = ${u.hasRefuse} \n")
                }

                findViewById<TextView>(R.id.result_tv).setText(resultSb.toString())

            }



        }
    }
}
